<%@page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!-- public quote -->
<%@include file="/jsp/comm_css_include.jsp"%>

<!-- zTreeStyle.css -->
<link rel="stylesheet" type="text/css"
	href="${basePath}/css/zTreeStyle/zTreeStyle/zTreeStyle.css">
<!doctype html>
<html>
<head></head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight creditbody">
		<div class="iboxtitle">
			<span><i class="glyphicon glyphicon-map-marker"></i>&nbsp;系统管理&gt;基础信息管理&gt;业务单位</span>
			<a href="javascript:void(0)"></a>
		</div>
		<div class="ibox-content">
			<button type="button" class="btn dpbtn_green" onclick="add();">新增</button>
			<button type="button" class="btn dpbtn_blue" onclick="update();">修改</button>
			<button type="button" class="btn dpbtn_yellow" onclick="disable();">禁用</button>
			<button type="button" class="btn dpbtn_red" onclick="enable();">启用</button>
			<button type="button" class="btn dpbtn_red" onclick="del();">删除</button>
		</div>

		<div class="ibox-content power"
			style="overflow: auto; overflow-x: hidden;">
			<div id="treeDemo" class="ztree">&nbsp;</div>
		</div>
	</div>


	<!-- public quote -->
	<%@include file="/jsp/comm_js_include.jsp"%>

	<!--zTreeStyle.js-->
	<script type="text/javascript"
		src="${basePath}/js/zTreeStyle/jquery.ztree.core.js" charset="utf-8"></script>
	<script type="text/javascript"
		src="${basePath}/js/zTreeStyle/jquery.ztree.excheck.js"
		charset="utf-8"></script>

	<script type="text/javascript">
    var serviceName = "组织机构";
    var addUrl = 'baseBusiUnitAdd.jsp';
    var delUrl = '${basePath}/BaseBusiUnitController/remove';
    var listUrl = '${basePath}/BaseBusiUnitController/list';

    //新增字典
    var winWidth = $(window).width();
	var winHeight = $(window).height();
	
    jQuery(function($) {  
    	getData();
    	$(".power").css({"height":winHeight / 1.3+"px"});
    });
    
    
    var setting = {
   	      view: {
   	        selectedMulti: false,
   	    	nameIsHTML: true  
   	      },
   	      data: {
   	        key: {
   	          title:""
   	        },
   	        simpleData: {
   	          enable: true
   	        }
   	      },
   	      callback: {
   	        beforeClick: beforeClick
   	      }
    };
    
    
    //菜单父级的编号
    var parentId = "";
    
    //菜单父级的名称
    var parentName = "";

    //机构编码
    var orgCode="";
    var parentCode="";
    
    function beforeClick(treeId, treeNode)
    {
    	parentId = treeNode.id;
    	parentName = treeNode.name;
    	orgCode=treeNode.orgCode;
    	parentCode=treeNode.parentCode;
    	if(!$.isEmpty({"field":parentId}))
    	{
    		layer.msg("请选择要删除权限！", {icon: 5});
    		return;
    	}

    	
    }
    var getData = function()
    {
    	$.dpAjax({
    		url:listUrl,
   			data:{},
   			success: function(datas){
    		  if(datas.flag == "100100")
			  {
    				  var  arr = [];
    				  var d = datas.result.rows;
    				  for(var i=0;i<d.length;i++)
    				  {  
    					  var r = d[i];
    					  var objs = {};
    					  objs.id = r.replaceId;
    					  objs.pId = r.parentId;
    					  objs.orgCode=r.orgCode;
    					  objs.parentCode=r.parentCode;
    					  
    					  if(null != r.orgName && r.orgName != "" && r.orgName != "undefined" && r.orgName != undefined)
    					  {
    						  if(r.dpStatus == 1)
        					  {
        						  objs.name = r.orgName;
        					  }
        					  else if(r.dpStatus == 2)
        					  {
        						  objs.name = r.orgName + '<span style="color:#f05050;">(已被禁用)</span>';
        					  }
    						  
    						  if(objs.name && null != objs.name && objs.name != "" && objs.name != 'undefined')
    						  {
    							  objs.open = true;
            					  arr.push(objs);
    						  }
    					  }
    					  
    					  if(r.replaceId == 1){
	            		  	objs.open = true;
     					  } else {
     						objs.open = false; 
     					  }
    				  }  
   				  $.fn.zTree.init($("#treeDemo"), setting, arr);
				  return;
			  }
			  if(datas.flag == "100200")
                 {
				  $.fn.showDatas({"datas":datas});
               	  return;
                 }
			  layer.msg(datas.message, {icon: 2});
               }
    	});
    }
    
	var addlayer;
    var add=function()
    {	
		
		if(!$.isEmpty({"field":parentId}))
		{
			layer.msg("请先选择节点再进行增加！", {icon: 5});
			return;
		}
	
		addlayer=layer.open({
	    	  type: 2,
	    	  title: '新增业务单位',
	    	  shadeClose: true,
	    	  shade: 0.8,
	    	  area: ['480px', '65%'],
	    	  content: '${basePath}/BaseBusiUnitViewController/toSave?parentId='+parentId+'&parentName='+parentName+'&orgCode='+orgCode,
	    	  end: function () 
	    	  {
	    		  refresh();
	          }
    	});
    	
    }
    
	var updatelayer;
    var update = function()
    {
    	if(!$.isEmpty({"field":parentId}))
		{
			layer.msg("请先选择节点再进行修改！", {icon: 5});
			return;
		}
    	if(parentId == 1)
     	 {
  			layer.msg("顶级父节点不能修改！", {icon: 5});
  			return;
      	}
    	updatelayer=layer.open({
	    	  type: 2,
	    	  title: '修改业务单位名称',
	    	  shadeClose: true,
	    	  shade: 0.8,
	    	  area: ['480px', '55%'],
	    	  content: '${basePath}/BaseBusiUnitViewController/toUpdate?parentId=' + parentId,
	    	  end: function () 
	    	  {
	    		  refresh();
	          }
    	});
    }
    

   
	//删除
    var del = function()
    {

    	if(!$.isEmpty({"field":parentId}))
    	{
    		layer.msg("请选择要删除权限！", {icon: 5});
    		return;
    	}
        
         var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
  
    	     srcNode = zTree.getSelectedNodes(),

    	     treeNode = srcNode[0];

    	 if(treeNode.isParent)
     	 {
     		layer.msg("父节点不能删除！", {icon: 5});
     		return;
     	 }
		
    	 if(parentId == 1)
	      {
	   		layer.msg("顶级父节点不能删除！", {icon: 5});
	   		return;
	      }
		layer.confirm('您确认要删除节点吗？', {
  			  btn: ['确定','取消'] //按钮
  			},function(){
  				layer.closeAll();
            $.dpAjax({
 	    		url:delUrl,
 	    		data:
 	    		{
     				"id":parentId
     			},
     			success: function(datas)
     			{
	     			  if(datas.flag == "100100")
					  {
	     				  layer.msg("执行成功！", {icon: 6});
	     				  refresh();
						  return;
					  }
					  if(datas.flag == "100200")
	                  {
						  $.fn.showDatas({"datas":datas});
	                	  return;
	                  }
					  layer.msg(datas.message, {icon: 2});
                 }
 	    	});
  		});	
    }
    
  //禁用
    var disable = function()
    {
    	
    	if(!$.isEmpty({"field":parentId}))
    	{
    		layer.msg("请选择要禁用的权限！", {icon: 5});
    		return;
    	}
        
	   
    	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
    	  
	   	     srcNode = zTree.getSelectedNodes(),
	
	   	     treeNode = srcNode[0];
    	
    	var dpStatus = treeNode.dpStatus;
    	
    	if(dpStatus == 2)
    	 {
    		layer.msg("菜单已被禁用，请勿重复禁用！", {icon: 5});
    		return;
    	 }
	
    	if(parentId == 1)
	      {
    		layer.msg("顶级父节点不能禁用！", {icon: 5});
    		return;
	      }

		layer.confirm('您确认要禁用吗？', {
  			  btn: ['确定','取消'] //按钮
  			},function(){
  				layer.closeAll();
            $.dpAjax({
 	    		url:'${basePath}/BaseBusiUnitController/disable',
 	    		data:
 	    		{
     				"id":parentId,
     			},
     			success: function(datas)
     			{
	     			  if(datas.flag == "100100")
					  {
	     				  layer.msg("执行成功！", {icon: 6});
	     				  refresh();
						  return;
					  }
					  if(datas.flag == "100200")
	                  {
						  $.fn.showDatas({"datas":datas});
	                	  return;
	                  }
					  layer.msg(datas.message, {icon: 2});
                 }
 	    	});
  		});	
    }
    
    
  //启用
    var enable = function()
    {
    	
    	if(!$.isEmpty({"field":parentId}))
    	{
    		layer.msg("请选择要启用的权限！", {icon: 5});
    		return;
    	}
        
	   
    	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
    	  
	   	     srcNode = zTree.getSelectedNodes(),
	
	   	     treeNode = srcNode[0];
    	
    	var dpStatus = treeNode.dpStatus;
    	
    	
    	if(parentId == 1)
	      {
  			layer.msg("顶级父节点不可操作！", {icon: 5});
  			return;
	      }
  	
	  	if(dpStatus == 1)
	  	 {
	  		layer.msg("菜单正常，请勿重复启用！", {icon: 5});
	  		return;
	  	 }

		layer.confirm('您确认要启用吗？', {
  			  btn: ['确定','取消'] //按钮
  			},function(){
  				layer.closeAll();
            $.dpAjax({
 	    		url:'${basePath}/BaseBusiUnitController/enable',
 	    		data:
 	    		{
     				"id":parentId,	
     			},
     			success: function(datas)
     			{
	     			  if(datas.flag == "100100")
					  {
	     				  layer.msg("执行成功！", {icon: 6});
	     				 refresh();
						  return;
					  }
					  if(datas.flag == "100200")
	                  {
						  $.fn.showDatas({"datas":datas});
	                	  return;
	                  }
					  layer.msg(datas.message, {icon: 2});
                 }
 	    	});
  		});	
    }
    

    var refresh = function()
    {	
    	 location.reload();
    }
</script>
</body>
</html>
